/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.CustomerBean;
import DAL.ExitsBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerCustomer", urlPatterns = {"/ControllerCustomer"})
public class ControllerCustomer extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        CustomerBean CTbean=new CustomerBean();
        ExitsBean exitbean=new ExitsBean();
        HttpSession session = request.getSession();
        if(request.getParameter("registry")!=null)
        {
            if(request.getParameter("registry").equals("Registry"))
            {
                    String user=request.getParameter("username");
                    String pass=request.getParameter("password");
                    String name=request.getParameter("fullname");
                    String gender=request.getParameter("gender");
                    String email=request.getParameter("email");
                    String Country=request.getParameter("country");
                    String address=request.getParameter("address");
                    String phone=request.getParameter("phone");
                    boolean CreateCT=CTbean.RegistryCustomer(user,pass,name,gender,email,Country,address,phone);
                    if(CreateCT == true)
                    {
                        session.setAttribute("para","Registry Success!");
                        response.sendRedirect("include/Notice.jsp");
                    }
                    else
                    {
                        out.println("<script>alert('Sorry! That username is already in use. Please enter a username other!');javascript:window.history.back(-1);</script>");
                    }
            }
        }
         if(request.getParameter("login")!=null)
        {
            if(request.getParameter("login").equals("Login"))
            {
                    String user=request.getParameter("user");
                    String pass=request.getParameter("pass");
                    boolean login=CTbean.LoginCustomer(user,pass);
                    if(login==true)
                    {
                        session.setAttribute("para","Login Success!");
                        session.setAttribute("User",user);
                        response.sendRedirect("include/Notice.jsp");
                       //out.println("<script>alert('Login cuccess!');window.location='index.jsp';</script>");
                    }
                    else
                    {
                        //System.out.println("<a href='index.jsp?page=Registry'>Login fail back...!</a>");
                        out.println("<script>alert('Username or password not correct!');javascript:window.history.back(-1);</script>");
                    }
            }
        }
        if(request.getParameter("delete")!=null)
        {
               if(request.getParameter("delete").equals("Delete"))
               {
                   String user=request.getParameter("username");
                    if(exitbean.ExitsCusInCarBook(user)==true)
                    {
                        out.println("<script>alert('You cannot delete! because this customer exist CarBookings table!');window.location='Admin/management.jsp?page=CarBooking';</script>");
                    }
                    else if(exitbean.ExitsCusInTourBook(user)==true)
                    {
                       out.println("<script>alert('You cannot delete! because this customer exist TourBookings table!');window.location='Admin/management.jsp?page=TourBooking';</script>");
                    }
                    else
                    {   
                       if(CTbean.DeleteCustomer(user)==true)
                       {
                           out.println("<script>alert('Delete Customer Success!');window.location='Admin/management.jsp?page=Customers';</script>");
                       }
                       else
                       {
                           //System.out.println("<a href='management.jsp?page=Customers'>Delete fail back...!</a>");
                           out.println("<script>alert('Delete customer fail!');javascript:window.history.back(-1);</script>");
                       }
                    }
               }
        }
        if(request.getParameter("Rg")!=null)
        {
            if(request.getParameter("Rg").equals("Registry"))
            {
                String user=request.getParameter("user");               
                    boolean ct=CTbean.CheckExitCustomer(user);
                    if(ct==true)
                    {
                        response.getWriter().write("That username is already in use");
                    }
                    else
                    {
                        response.getWriter().write("Username is valid and not in use");
                    }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
